package com.itheima.utils;

import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.*;
import com.aliyun.teautil.models.*;
import com.aliyun.dysmsapi20170525.Client;

/**
 * 短信发送工具类
 */
public class SMSUtils {

	private static final String ACCESS_KEY_ID = "LTAIsYwabfHlScQN";//秘钥ID
	private static final String ACCESS_KEY_SECRET = "mNXYbJuv9Yk5fS1eVH0ujnLZ8RtEE2";//秘钥
	private static final String signName= "阿里云短信测试";//短信签名
	private static final String template_Code= "SMS_154950909";//短信模板
	private static final String endpoint="dysmsapi.aliyuncs.com";// 访问的域名

	/**
	 * 发送短信
	 * @param phone 手机号
	 * @param code 验证码
	 * @throws Exception
	 */
	public static void sendCode(String phone,String code) throws Exception {
		Client client = SMSSendCode.createClient(
				ACCESS_KEY_ID,//秘钥ID：ACCESS_KEY_ID
				ACCESS_KEY_SECRET);//秘钥：ACCESS_KEY_SECRET
		SendSmsRequest sendSmsRequest = new SendSmsRequest()
				.setSignName(signName)//短信签名
				.setTemplateCode(template_Code)//模板code
				.setPhoneNumbers(phone)//手机号
				.setTemplateParam("{\"code\":"+code+"}");//验证码填充
		RuntimeOptions runtime = new RuntimeOptions();
		SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
		com.aliyun.teaconsole.Client.log(Common.toJSONString(TeaModel.buildMap(resp)));
	}

}
